#include "stdafx.h"
#include "ISub.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const ISub::FunctionNameISub = "Subtract";

ISub::ISub(void) 
{
	_fnApi.SetFunctionName(ISub::FunctionNameISub);
	_fnApi.SetFunctionDescription("This function subtracts a image on top of the solution.");
									//inputs
	_fnApi.AddParamDef(new ProcParamDefString(0, "Image", "Sets the Image to be subtracted. Make sure the dimensions are the same as that of the image you're subtracting from. Enter the path to the image", ProcParamDef::input, 0, 500 ));
									//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


ISub::~ISub(void)
{

}


bool ISub::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		
		ProcParamString *source = dynamic_cast<ProcParamString *>(step->GetInputs()->GetParam(0));	//Check the Param number

		if  (source != '\0')
		{
			cv::Mat output;
			cv::Mat input = cv::imread(step->GetIntermediateImagePathFilename());	
			cv::Mat input2 = cv::imread((std::string)source->GetValueStr());

			if (input.cols == input2.cols && input.rows == input2. rows)
			{
				cv::subtract(input, input2, output);

				cv::imwrite(step->GetIntermediateImagePathFilename(), output);
				
				input.release();
				input.release();
				output.release();
				rc = true;
			}
		}
	}
	return rc;
}

